{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Merge Two 2D Arrays by Summing Values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #two-pointers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #双指针"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: mergeArrays"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #合并两个二维数组 - 求和法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你两个 <strong>二维</strong> 整数数组 <code>nums1</code> 和 <code>nums2.</code></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>nums1[i] = [id<sub>i</sub>, val<sub>i</sub>]</code> 表示编号为 <code>id<sub>i</sub></code> 的数字对应的值等于 <code>val<sub>i</sub></code> 。</li>\n",
    "\t<li><code>nums2[i] = [id<sub>i</sub>, val<sub>i</sub>]</code>&nbsp;表示编号为 <code>id<sub>i</sub></code> 的数字对应的值等于 <code>val<sub>i</sub></code> 。</li>\n",
    "</ul>\n",
    "\n",
    "<p>每个数组都包含 <strong>互不相同</strong> 的 id ，并按 id 以 <strong>递增</strong> 顺序排列。</p>\n",
    "\n",
    "<p>请你将两个数组合并为一个按 id 以递增顺序排列的数组，并符合下述条件：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>只有在两个数组中至少出现过一次的 id 才能包含在结果数组内。</li>\n",
    "\t<li>每个 id 在结果数组中 <strong>只能出现一次</strong> ，并且其对应的值等于两个数组中该 id 所对应的值求和。如果某个数组中不存在该 id ，则认为其对应的值等于 <code>0</code> 。</li>\n",
    "</ul>\n",
    "\n",
    "<p>返回结果数组。返回的数组需要按 id 以递增顺序排列。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums1 = [[1,2],[2,3],[4,5]], nums2 = [[1,4],[3,2],[4,1]]\n",
    "<strong>输出：</strong>[[1,6],[2,3],[3,2],[4,6]]\n",
    "<strong>解释：</strong>结果数组中包含以下元素：\n",
    "- id = 1 ，对应的值等于 2 + 4 = 6 。\n",
    "- id = 2 ，对应的值等于 3 。\n",
    "- id = 3 ，对应的值等于 2 。\n",
    "- id = 4 ，对应的值等于5 + 1 = 6 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums1 = [[2,4],[3,6],[5,5]], nums2 = [[1,3],[4,3]]\n",
    "<strong>输出：</strong>[[1,3],[2,4],[3,6],[4,3],[5,5]]\n",
    "<strong>解释：</strong>不存在共同 id ，在结果数组中只需要包含每个 id 和其对应的值。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums1.length, nums2.length &lt;= 200</code></li>\n",
    "\t<li><code>nums1[i].length == nums2[j].length == 2</code></li>\n",
    "\t<li><code>1 &lt;= id<sub>i</sub>, val<sub>i</sub> &lt;= 1000</code></li>\n",
    "\t<li>数组中的 id 互不相同</li>\n",
    "\t<li>数据均按 id 以严格递增顺序排列</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [merge-two-2d-arrays-by-summing-values](https://leetcode.cn/problems/merge-two-2d-arrays-by-summing-values/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [merge-two-2d-arrays-by-summing-values](https://leetcode.cn/problems/merge-two-2d-arrays-by-summing-values/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[1,2],[2,3],[4,5]]\\n[[1,4],[3,2],[4,1]]', '[[2,4],[3,6],[5,5]]\\n[[1,3],[4,3]]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        index1 = 0\n",
    "        index2 =0\n",
    "        res = []\n",
    "\n",
    "        while index1 < len(nums1) and index2 < len(nums2):\n",
    "            if nums1[index1][0] == nums2[index2][0]:\n",
    "                res.append([nums1[index1][0],nums1[index1][1]+nums2[index2][1]])\n",
    "                index1+=1\n",
    "                index2+=1\n",
    "            elif nums1[index1][0] < nums2[index2][0]:\n",
    "                res.append(nums1[index1])\n",
    "                index1+=1\n",
    "            else:\n",
    "                res.append(nums2[index2])\n",
    "                index2+=1\n",
    "        res+=nums1[index1:]\n",
    "        res+=nums2[index2:]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, a: List[List[int]], b: List[List[int]]) -> List[List[int]]:\n",
    "        ans = []\n",
    "        i, n = 0, len(a)\n",
    "        j, m = 0, len(b)\n",
    "        while True:\n",
    "            if i == n:\n",
    "                ans.extend(b[j:])\n",
    "                return ans\n",
    "            if j == m:\n",
    "                ans.extend(a[i:])\n",
    "                return ans\n",
    "            if a[i][0] < b[j][0]:\n",
    "                ans.append(a[i])\n",
    "                i += 1\n",
    "            elif a[i][0] > b[j][0]:\n",
    "                ans.append(b[j])\n",
    "                j += 1\n",
    "            else:\n",
    "                a[i][1] += b[j][1]\n",
    "                ans.append(a[i])\n",
    "                i += 1\n",
    "                j += 1\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        i, j = 0, 0\n",
    "        ans = []\n",
    "        n1, n2 = len(nums1), len(nums2)\n",
    "        while i < n1 and j < n2:\n",
    "            num1, num2 = nums1[i][0], nums2[j][0]\n",
    "            if num1 == num2:\n",
    "                ans.append([num1, nums1[i][1]+nums2[j][1]])\n",
    "                i += 1\n",
    "                j += 1\n",
    "            elif num1 > num2:\n",
    "                ans.append([num2, nums2[j][1]])\n",
    "                j += 1\n",
    "            else:\n",
    "                ans.append([num1, nums1[i][1]])\n",
    "                i += 1\n",
    "        while i < n1:\n",
    "            ans.append([nums1[i][0], nums1[i][1]])\n",
    "            i += 1\n",
    "        while j < n2:\n",
    "            ans.append([nums2[j][0], nums2[j][1]])\n",
    "            j += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        d1 = [0] * 1001\n",
    "        for k,v in nums1:\n",
    "            d1[k] += v\n",
    "        for k,v in nums2:\n",
    "            d1[k] += v\n",
    "        # ans = []\n",
    "        # for k,v in enumerate(d1):\n",
    "        #     if v:\n",
    "        #         ans.append([k,v])\n",
    "        return [[k,v] for k,v in enumerate(d1) if v]\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        log = Counter()\n",
    "        for x, y in nums1 + nums2: log[x] += y\n",
    "        return sorted(log.items())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        c = Counter()\n",
    "        for i, x in nums1:\n",
    "            c[i] = x\n",
    "        for j, y in nums2:\n",
    "            c[j] += y\n",
    "        return sorted([[k, v] for k, v in c.items()])\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        newList = []\n",
    "        while len(nums1) > 0 or len(nums2) > 0:\n",
    "            case = 0\n",
    "            if len(nums2) == 0 and len(nums1) > 0:\n",
    "                case = 1\n",
    "            elif len(nums1) == 0 and len(nums2) > 0:\n",
    "                case = 2\n",
    "            elif nums1[0][0] < nums2[0][0]:\n",
    "                case = 1\n",
    "            elif nums1[0][0] > nums2[0][0]:\n",
    "                case = 2\n",
    "            elif nums1[0][0] == nums2[0][0]:\n",
    "                case = 3\n",
    "\n",
    "            if case == 1:\n",
    "                newList.append(nums1[0])\n",
    "                del nums1[0]\n",
    "            elif case == 2:\n",
    "                newList.append(nums2[0])\n",
    "                del nums2[0]\n",
    "            elif case == 3:\n",
    "                newList.append([nums1[0][0], nums1[0][1] + nums2[0][1]])\n",
    "                del nums1[0]\n",
    "                del nums2[0]\n",
    "        return newList"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        ans = list()\n",
    "        i, j = 0, 0\n",
    "        while i < len(nums1) or j < len(nums2):\n",
    "            if j >= len(nums2):\n",
    "                ans.extend(nums1[i:])\n",
    "                break\n",
    "            if i >= len(nums1):\n",
    "                ans.extend(nums2[j:])\n",
    "                break\n",
    "            if nums1[i][0] < nums2[j][0]:\n",
    "                ans.append(nums1[i])\n",
    "                i += 1\n",
    "            elif nums1[i][0] == nums2[j][0]:\n",
    "                ans.append([nums1[i][0], nums1[i][1] + nums2[j][1]])\n",
    "                i += 1\n",
    "                j += 1\n",
    "            else:\n",
    "                ans.append(nums2[j])\n",
    "                j += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:       \n",
    "        d = Counter()\n",
    "        for x, y in nums1: d[x] += y\n",
    "        for x, y in nums2: d[x] += y\n",
    "        return sorted(d.items())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        res = {}\n",
    "        nums1.extend(nums2)\n",
    "        for num in nums1:\n",
    "            res[num[0]] = res.get(num[0], 0) + num[1]\n",
    "        return sorted([[key, value] for key, value in res.items()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        count = Counter()\n",
    "        for x, y in nums1 + nums2:\n",
    "            count[x] += y\n",
    "        return sorted(count.items())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        result = []\n",
    "        i, j = 0, 0\n",
    "\n",
    "        while i < len(nums1) or j < len(nums2):\n",
    "            id1 = nums1[i][0] if i < len(nums1) else float('inf')\n",
    "            id2 = nums2[j][0] if j < len(nums2) else float('inf')\n",
    "\n",
    "            if id1 == id2:\n",
    "                result.append([id1, nums1[i][1] + nums2[j][1]])\n",
    "                i += 1\n",
    "                j += 1\n",
    "            elif id1 < id2:\n",
    "                result.append([id1, nums1[i][1]])\n",
    "                i += 1\n",
    "            else:\n",
    "                result.append([id2, nums2[j][1]])\n",
    "                j += 1\n",
    "\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        nums1.extend(nums2)\n",
    "        hash_s = set()\n",
    "        hash_d = {}\n",
    "        for i in nums1:\n",
    "            hash_d[i[0]] = hash_d.get(i[0],0) + i[1]\n",
    "        num = [[k,v] for k, v in hash_d.items()]\n",
    "        return sorted(num,key = lambda x:x[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        i = j = 0\n",
    "        rv = []\n",
    "        while i<len(nums1) and j<len(nums2):\n",
    "            p, x = nums1[i]\n",
    "            q, y = nums2[j]\n",
    "            if p==q:\n",
    "                rv.append([p,x+y])\n",
    "                i += 1\n",
    "                j += 1\n",
    "            elif p<q:\n",
    "                rv.append([p,x])\n",
    "                i += 1\n",
    "            else:\n",
    "                rv.append([q,y])\n",
    "                j += 1\n",
    "        while i<len(nums1):\n",
    "            p, x = nums1[i]\n",
    "            rv.append([p,x])\n",
    "            i += 1\n",
    "        while j<len(nums2):\n",
    "            q, y = nums2[j]\n",
    "            rv.append([q,y])\n",
    "            j += 1\n",
    "        return rv "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        m = {}\n",
    "        for xs in nums1:\n",
    "            m[xs[0]] = xs\n",
    "        for xs in nums2:\n",
    "            if xs[0] in m:\n",
    "                m[xs[0]][1] += xs[1]\n",
    "            else:\n",
    "                m[xs[0]] = xs\n",
    "\n",
    "        a=sorted(m)\n",
    "        array = []\n",
    "        for index in a:\n",
    "            array.append(m[index])\n",
    "        return array\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        log = Counter()\n",
    "        for x, y in nums1 + nums2: log[x] += y\n",
    "        return sorted(log.items())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        nums1.extend(nums2)\n",
    "        hash_d = {}\n",
    "        for i in nums1:\n",
    "            hash_d[i[0]] = hash_d.get(i[0],0) + i[1]\n",
    "        num = [[k,v] for k, v in hash_d.items()]\n",
    "        return sorted(num,key = lambda x:x[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        sumDict = defaultdict(int)\n",
    "        for idx, val in nums1 + nums2:\n",
    "            sumDict[idx] += val\n",
    "        return sorted(list(sumDict.items()), key=lambda x:x[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        d = {}\n",
    "        for x, y in nums1:\n",
    "            d[x] = y\n",
    "        for x, y in nums2:\n",
    "            d[x] = d.get(x, 0) + y\n",
    "        res = []\n",
    "        for x in d:\n",
    "            res.append([x, d[x]])\n",
    "        res.sort()\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        cnt = {}\n",
    "        for i in range(len(nums1)):\n",
    "            cnt[nums1[i][0]] = nums1[i][1] if nums1[i][0] not in cnt else cnt[nums1[i][0]] + nums1[i][1]\n",
    "        for i in range(len(nums2)):\n",
    "            cnt[nums2[i][0]] = nums2[i][1] if nums2[i][0] not in cnt else cnt[nums2[i][0]] + nums2[i][1]\n",
    "        ans = []\n",
    "        for key, value in cnt.items():\n",
    "            ans.append([key, value])\n",
    "        ans.sort(key=lambda x:x[0])\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        a={i[0]:i[1] for i in nums1}\n",
    "        b={i[0]:i[1] for i in nums2}\n",
    "        for i in a.keys():\n",
    "            if i in b:\n",
    "                b[i]+=a[i]\n",
    "            else:\n",
    "                b[i]=a[i]\n",
    "        b=sorted(b.items(),key=lambda x:x[0])\n",
    "        return [list(i) for i in b]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        result=[]\n",
    "        i=0\n",
    "        j=0\n",
    "        while i <len(nums1) and j<len(nums2):\n",
    "            if nums1[i][0]<nums2[j][0]:\n",
    "                result.append(nums1[i])\n",
    "                i=i+1\n",
    "                continue\n",
    "            elif nums1[i][0]==nums2[j][0]:\n",
    "                result.append([nums1[i][0],(nums1[i][1]+nums2[j][1])])\n",
    "                i=i+1\n",
    "                j=j+1\n",
    "                continue\n",
    "            if nums1[i][0]>nums2[j][0]:\n",
    "                result.append(nums2[j])\n",
    "                j=j+1\n",
    "        if i<len(nums1):\n",
    "            for e in nums1[i::]:\n",
    "                result.append(e)\n",
    "        elif j<len(nums2):\n",
    "            for e in nums2[j::]:\n",
    "                result.append(e)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        i, j = 0, 0\n",
    "        ans = []\n",
    "        n1, n2 = len(nums1), len(nums2)\n",
    "        while i < n1 and j < n2:\n",
    "            num1, num2 = nums1[i][0], nums2[j][0]\n",
    "            if num1 == num2:\n",
    "                ans.append([num1, nums1[i][1]+nums2[j][1]])\n",
    "                i += 1\n",
    "                j += 1\n",
    "            elif num1 > num2:\n",
    "                ans.append([num2, nums2[j][1]])\n",
    "                j += 1\n",
    "            else:\n",
    "                ans.append([num1, nums1[i][1]])\n",
    "                i += 1\n",
    "        while i < n1:\n",
    "            ans.append([nums1[i][0], nums1[i][1]])\n",
    "            i += 1\n",
    "        while j < n2:\n",
    "            ans.append([nums2[j][0], nums2[j][1]])\n",
    "            j += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        index_1 = 0\n",
    "        index_2 = 0\n",
    "        output = []\n",
    "\n",
    "        while index_1 < len(nums1) and index_2 < len(nums2):\n",
    "            # get id and value\n",
    "            id1, val1 = nums1[index_1]\n",
    "            id2, val2 = nums2[index_2]\n",
    "            if id1 < id2:\n",
    "                output.append([id1, val1])\n",
    "                index_1 +=1\n",
    "            elif id1 > id2:\n",
    "                output.append([id2, val2])\n",
    "                index_2 +=1\n",
    "            else:\n",
    "                val_sum = val1+val2\n",
    "                output.append([id1, val_sum])\n",
    "                index_1 += 1\n",
    "                index_2 +=1\n",
    "\n",
    "        if index_1 == len(nums1):\n",
    "            output.extend(nums2[index_2::])\n",
    "        elif index_2 == len(nums2):\n",
    "            output.extend(nums1[index_1::])\n",
    "        \n",
    "        return output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        c = Counter()\n",
    "        for x, y in nums1:\n",
    "            c[x] += y\n",
    "        for x, y in nums2:\n",
    "            c[x] += y\n",
    "        return list(sorted(c.items()))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "          return list(sorted((Counter(dict(nums1)) + Counter(dict(nums2))).items(), key=lambda m: m[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        sz1, sz2 = len(nums1), len(nums2)\n",
    "        i, j = 0, 0\n",
    "        res = []\n",
    "        while i < sz1 and j < sz2:\n",
    "            if nums1[i][0] < nums2[j][0]:\n",
    "                res.append(nums1[i])\n",
    "                i += 1\n",
    "            elif nums1[i][0] == nums2[j][0]:\n",
    "                nums1[i][1] += nums2[j][1]\n",
    "                res.append(nums1[i])\n",
    "                i += 1\n",
    "                j += 1\n",
    "            else:\n",
    "                res.append(nums2[j])\n",
    "                j += 1\n",
    "        \n",
    "        if i < sz1:\n",
    "            res.extend(nums1[i:])\n",
    "        if j < sz2:\n",
    "            res.extend(nums2[j:])\n",
    "        \n",
    "        return res\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\r\n",
    "        temp = dict()\r\n",
    "        for i in nums1:\r\n",
    "            temp[i[0]] = i[1]\r\n",
    "        for j in nums2:\r\n",
    "            if j[0] in temp.keys():\r\n",
    "                temp[j[0]] += j[1]\r\n",
    "            else:\r\n",
    "                temp[j[0]] = j[1]\r\n",
    "        ans = list()\r\n",
    "        for a in sorted(temp.keys()):\r\n",
    "            ans.append([a,temp[a]])\r\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        c=Counter()\n",
    "        for i,num in nums1:\n",
    "            c[i]+=num\n",
    "        for i,num in nums2:\n",
    "            c[i]+=num\n",
    "        return sorted(c.items())\n",
    "\n",
    "        \n",
    "\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        nums1.extend(nums2)\n",
    "        hash_d = {}\n",
    "        for i in nums1:\n",
    "            hash_d[i[0]] = hash_d.get(i[0],0) + i[1]\n",
    "        num = [[k,v] for k, v in hash_d.items()]\n",
    "        return sorted(num,key = lambda x:x[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        ans, m, n = [], len(nums1), len(nums2)\n",
    "        cnt = defaultdict(int)\n",
    "        for x, y in nums1:\n",
    "            cnt[x] += y\n",
    "        for x, y in nums2:\n",
    "            cnt[x] += y\n",
    "        return sorted(list(cnt.items()))\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        dictsum = {}\n",
    "        for x,y in nums1:\n",
    "            dictsum[x] = y\n",
    "        for x,y in nums2:\n",
    "            if x in dictsum:\n",
    "                dictsum[x] += y\n",
    "            else:\n",
    "                dictsum[x] = y\n",
    "        return [[x,y] for x,y in sorted(dictsum.items(), key=lambda item: item[0])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        i, j = 0, 0\n",
    "        ans = []\n",
    "        while i < len(nums1) and j < len(nums2):\n",
    "            if nums1[i][0] == nums2[j][0]:\n",
    "                ans += [[nums1[i][0], nums1[i][1] + nums2[j][1]]]\n",
    "                i += 1\n",
    "                j += 1\n",
    "            elif nums1[i][0] < nums2[j][0]:\n",
    "                ans += [nums1[i]]\n",
    "                i += 1\n",
    "            else:\n",
    "                ans += [nums2[j]]\n",
    "                j += 1\n",
    "        if i < len(nums1):\n",
    "            ans += nums1[i:]\n",
    "        elif j < len(nums2):\n",
    "            ans += nums2[j:]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        d = defaultdict(int)\n",
    "        for k, v in nums1:\n",
    "            d[k] += v\n",
    "        for k, v in nums2:\n",
    "            d[k] += v\n",
    "        ans = [[k, v] for k, v in d.items()]\n",
    "        ans.sort()\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        nums1.extend(nums2)\n",
    "        hash_s = set()\n",
    "        hash_d = {}\n",
    "        for i in nums1:\n",
    "            hash_d[i[0]] = hash_d.get(i[0],0) + i[1]\n",
    "        num = [[k,v] for k, v in hash_d.items()]\n",
    "        return sorted(num,key = lambda x:x[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        # 创建一个字典来累加每个id的值\n",
    "        value_map = {}\n",
    "\n",
    "        # 遍历nums1和nums2并累加每个id的值\n",
    "        for id_val in nums1:\n",
    "            value_map[id_val[0]] = id_val[1]\n",
    "\n",
    "        for id_val in nums2:\n",
    "            # 如果id在字典中，累加值，否则将该id和值添加到字典中\n",
    "            if id_val[0] in value_map:\n",
    "                value_map[id_val[0]] += id_val[1]\n",
    "            else:\n",
    "                value_map[id_val[0]] = id_val[1]\n",
    "\n",
    "        # 从字典中提取排序后的结果\n",
    "        res = sorted([[k, v] for k, v in value_map.items()], key=lambda x: x[0])\n",
    "\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        \"\"\"\n",
    "        res = []\n",
    "        n = max(nums1[-1][1], nums2[-1][1]\n",
    "        for i in range(n):\n",
    "            if nums1[i][0] - i != 1:\n",
    "                nums1.extend([i+1,0])\n",
    "                continue\n",
    "        for i in range(n):\n",
    "            if nums2[i][0] - i != 1:\n",
    "                nums2.extend([i+1,0])\n",
    "                continue\n",
    "        for i in range(n):\n",
    "            res.extend([i+1, nums1[i][1]+nums2[i][1]])\n",
    "        return res\n",
    "        \"\"\"\n",
    "        hp = {}\n",
    "        for id, value in nums1:\n",
    "            if id not in hp.keys():\n",
    "                hp[id] = value\n",
    "            else:\n",
    "                hp[id] += value\n",
    "        for id2, value2 in nums2:\n",
    "            if id2 not in hp.keys():\n",
    "                hp[id2] = value2\n",
    "            else:\n",
    "                hp[id2] += value2\n",
    "        res = sorted(hp.items(), key=lambda d: d[0], reverse=False)\n",
    "        return res\n",
    "        \n",
    "\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        dic=[0]*1001\n",
    "        for i in nums1:\n",
    "            dic[i[0]]=dic[i[0]]+i[1]\n",
    "        for i in nums2:\n",
    "            dic[i[0]]=dic[i[0]]+i[1]\n",
    "        ans=[]\n",
    "        for i,x in enumerate(dic):\n",
    "            if x!=0:\n",
    "                ans.append([i,x])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        m = Counter() #字典解析 利用字典唯一key的特性保存数组的起始索引 相同的则增加 不同的则加入这个key\n",
    "        for x,y in nums1+nums2: m[x]+=y\n",
    "        # 对字典的key进行排序\n",
    "        return sorted(m.items())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        d = {}\n",
    "        chu = []\n",
    "        for i in nums1:\n",
    "            d[i[0]] = i[1]\n",
    "        for i in nums2:\n",
    "            try:\n",
    "                d[i[0]] += i[1]\n",
    "            except:\n",
    "                d[i[0]] = i[1]\n",
    "        for a,b in d.items():\n",
    "            chu.append([a,b])\n",
    "        chu.sort(key = lambda x:x[0])\n",
    "        return chu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        fh = []\n",
    "        x = y = 0\n",
    "        a, b = len(nums1), len(nums2)\n",
    "        while x < a and y < b:\n",
    "            if nums1[x][0] < nums2[y][0]:\n",
    "                fh.append(nums1[x])\n",
    "                x += 1\n",
    "            elif nums1[x][0] == nums2[y][0]:\n",
    "                fh.append([nums1[x][0], nums1[x][1]+nums2[y][1]])\n",
    "                x += 1\n",
    "                y += 1\n",
    "            else:\n",
    "                fh.append(nums2[y])\n",
    "                y += 1\n",
    "        return fh + nums1[x:] + nums2[y:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        nums1.extend(nums2)\n",
    "        hash_d = {}\n",
    "        for i in nums1:\n",
    "            hash_d[i[0]] = hash_d.get(i[0],0) + i[1]\n",
    "        num = [[k,v] for k, v in hash_d.items()]\n",
    "        return sorted(num,key = lambda x:x[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, num1s, num2s):\n",
    "        num1l, num2l = len(num1s), len(num2s)\n",
    "        i, j = 0, 0\n",
    "        result = []\n",
    "        while i < num1l and j < num2l:\n",
    "            if num1s[i][0] < num2s[j][0]:\n",
    "                result.append(num1s[i])\n",
    "                i += 1\n",
    "            elif num1s[i][0] > num2s[j][0]:\n",
    "                result.append(num2s[j])\n",
    "                j += 1\n",
    "            else:\n",
    "                result.append([num1s[i][0], num1s[i][1] + num2s[j][1]])\n",
    "                i += 1\n",
    "                j += 1\n",
    "        if i < num1l:\n",
    "            result.extend(num1s[i:])\n",
    "        elif j < num2l:\n",
    "            result.extend(num2s[j:])\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        nums1.append([2000, 0])\n",
    "        nums2.append([2001, 0])\n",
    "        it1, it2 = iter(nums1), iter(nums2)\n",
    "        id2, n2 = next(it2)\n",
    "        res = []\n",
    "        for id1, n1 in nums1:\n",
    "            while id2 < id1:\n",
    "                res.append([id2, n2])\n",
    "                id2, n2 = next(it2)\n",
    "            res.append([id1, n1]) \n",
    "            if id1 == id2:\n",
    "                res[-1][1] += n2\n",
    "                id2, n2 = next(it2)\n",
    "        res.pop()\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        res,m,n=[],len(nums1),len(nums2)\n",
    "        i,j=0,0\n",
    "        while i<m and j<n:\n",
    "            if nums1[i][0]==nums2[j][0]:\n",
    "                t=nums1[i][1]+nums2[j][1]\n",
    "                res.append([nums1[i][0],t])\n",
    "                i+=1\n",
    "                j+=1\n",
    "            elif nums1[i][0]>nums2[j][0]:\n",
    "                res.append(nums2[j])\n",
    "                j+=1\n",
    "            elif nums1[i][0]<nums2[j][0]:\n",
    "                res.append(nums1[i])\n",
    "                i+=1\n",
    "\n",
    "        if i<m:\n",
    "            res.extend(nums1[i:])\n",
    "        if j<n:\n",
    "            res.extend(nums2[j:])\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        d = Counter()\n",
    "        for x, y in nums1: d[x] += y\n",
    "        for x, y in nums2: d[x] += y\n",
    "        return list(sorted(d.items()))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "\n",
    "        res = []\n",
    "\n",
    "        while nums1 and nums2:\n",
    "            if nums1[0][0] == nums2[0][0]:\n",
    "                res.append([nums1[0][0], nums1[0][1] + nums2[0][1]])\n",
    "                nums1.pop(0)\n",
    "                nums2.pop(0)\n",
    "            elif nums1[0][0] > nums2[0][0]:\n",
    "                res.append(nums2.pop(0))\n",
    "            else:\n",
    "                res.append(nums1.pop(0))\n",
    "        \n",
    "        if nums1:\n",
    "            res.extend(nums1)\n",
    "        if nums2:\n",
    "            res.extend(nums2)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        len1=len(nums1)\n",
    "        len2=len(nums2)\n",
    "        ans=[]\n",
    "        l=r=0\n",
    "        while True:\n",
    "            if l==len1:\n",
    "                ans+=nums2[r:]\n",
    "                return ans\n",
    "            if r==len2:\n",
    "                ans+=nums1[l:]\n",
    "                return ans\n",
    "            if nums1[l][0]==nums2[r][0]:\n",
    "                nums1[l][1]+=nums2[r][1]\n",
    "                ans.append(nums1[l])\n",
    "                l+=1\n",
    "                r+=1\n",
    "            elif nums1[l][0]>nums2[r][0]:\n",
    "                ans.append(nums2[r])\n",
    "                r+=1\n",
    "            else:\n",
    "                ans.append(nums1[l])\n",
    "                l+=1\n",
    "        return ans\n",
    "\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        dic = Counter()\n",
    "        for k, v in nums1 + nums2:\n",
    "            dic[k] += v\n",
    "        return sorted([[k, v] for k, v in dic.items()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        l1 = len(nums1)\n",
    "        l2 = len(nums2)\n",
    "        p1 = p2 = 0\n",
    "        while p1<l1 and p2 < l2:\n",
    "            if nums1[p1][0] == nums2[p2][0]:\n",
    "                nums1[p1][1] += nums2[p2][1]\n",
    "                p1 += 1\n",
    "                p2 += 1\n",
    "            elif nums1[p1][0] < nums2[p2][0]:\n",
    "                p1+=1\n",
    "            else:\n",
    "                nums1.append(nums2[p2])\n",
    "                p2 +=1\n",
    "\n",
    "        if p2 != l2:\n",
    "            nums1 += nums2[p2:]\n",
    "        \n",
    "        return sorted(nums1,key = lambda x:x[0])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        dic = defaultdict(int)\n",
    "        for num in nums1:\n",
    "            dic[num[0]] += num[1]\n",
    "        for num in nums2:\n",
    "            dic[num[0]] += num[1]\n",
    "        \n",
    "        return sorted([[k, v] for k, v in dic.items()], key=lambda x: x[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "\n",
    "        i =j = 0\n",
    "        ans=[]\n",
    "        n,m = len(nums1),len(nums2)\n",
    "        while i < n and j < m:\n",
    "            if nums1[i][0] < nums2[j][0]:\n",
    "                ans.append(nums1[i])\n",
    "                i+=1\n",
    "            elif nums1[i][0] > nums2[j][0]:\n",
    "                ans.append(nums2[j])\n",
    "                j+=1\n",
    "            else :\n",
    "                ans.append([nums1[i][0],nums1[i][1]+nums2[j][1]])\n",
    "                i+=1\n",
    "                j+=1\n",
    "        while j<m :\n",
    "            ans.append(nums2[j])\n",
    "            j+=1\n",
    "\n",
    "        while i<n:\n",
    "            ans.append(nums1[i])\n",
    "            i+=1\n",
    "\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        dic = defaultdict(int)\n",
    "        for num in nums1:\n",
    "            dic[num[0]] += num[1]\n",
    "        for num in nums2:\n",
    "            dic[num[0]] += num[1]\n",
    "        \n",
    "        res = []\n",
    "        for item in dic.items():\n",
    "            res.append([item[0], item[1]])\n",
    "\n",
    "        return sorted(res, key=lambda x: x[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        left1 = left2 = 0\n",
    "        n1 = len(nums1)\n",
    "        n2 = len(nums2)\n",
    "        ans =[]\n",
    "        while left1 < n1 and left2 < n2:\n",
    "            if nums1[left1][0] == nums2[left2][0]:\n",
    "                ans.append([nums1[left1][0], nums1[left1][1] + nums2[left2][1]])\n",
    "                left1 += 1\n",
    "                left2 += 1\n",
    "            elif nums1[left1][0] < nums2[left2][0]:\n",
    "                ans.append([nums1[left1][0], nums1[left1][1]])\n",
    "                left1 += 1\n",
    "            else:\n",
    "                ans.append([nums2[left2][0], nums2[left2][1]])\n",
    "                left2 += 1\n",
    "        while left1 < n1:\n",
    "            ans.append([nums1[left1][0], nums1[left1][1]])\n",
    "            left1 += 1\n",
    "        while left2 < n2:\n",
    "            ans.append([nums2[left2][0], nums2[left2][1]])\n",
    "            left2 += 1\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        left1 = left2 = 0\n",
    "        n1 = len(nums1)\n",
    "        n2 = len(nums2)\n",
    "        ans =[]\n",
    "        while left1 < n1 and left2 < n2:\n",
    "            if nums1[left1][0] == nums2[left2][0]:\n",
    "                ans.append([nums1[left1][0], nums1[left1][1] + nums2[left2][1]])\n",
    "                left1 += 1\n",
    "                left2 += 1\n",
    "            elif nums1[left1][0] < nums2[left2][0]:\n",
    "                ans.append([nums1[left1][0], nums1[left1][1]])\n",
    "                left1 += 1\n",
    "            else:\n",
    "                ans.append([nums2[left2][0], nums2[left2][1]])\n",
    "                left2 += 1\n",
    "        while left1 < n1:\n",
    "            ans.append([nums1[left1][0], nums1[left1][1]])\n",
    "            left1 += 1\n",
    "        while left2 < n2:\n",
    "            ans.append([nums2[left2][0], nums2[left2][1]])\n",
    "            left2 += 1\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        dic = defaultdict(int)\n",
    "        for num in nums1:\n",
    "            dic[num[0]] += num[1]\n",
    "        for num in nums2:\n",
    "            dic[num[0]] += num[1]\n",
    "        \n",
    "        return sorted([[k, v] for k, v in dic.items()], key=lambda x: x[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        combined = nums1 + nums2\n",
    "        id_sums = {}\n",
    "        for id,val in combined:\n",
    "            if id in id_sums:\n",
    "                id_sums[id]+=val\n",
    "            else:\n",
    "                id_sums[id]=val\n",
    "            result=[[id,val] for id,val in sorted(id_sums.items())]\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        i, j = 0, 0\n",
    "        ans = []\n",
    "        n1, n2 = len(nums1), len(nums2)\n",
    "        while i < n1 and j < n2:\n",
    "            num1, num2 = nums1[i][0], nums2[j][0]\n",
    "            if num1 == num2:\n",
    "                ans.append([num1, nums1[i][1]+nums2[j][1]])\n",
    "                i += 1\n",
    "                j += 1\n",
    "            elif num1 > num2:\n",
    "                ans.append([num2, nums2[j][1]])\n",
    "                j += 1\n",
    "            else:\n",
    "                ans.append([num1, nums1[i][1]])\n",
    "                i += 1\n",
    "        while i < n1:\n",
    "            ans.append([nums1[i][0], nums1[i][1]])\n",
    "            i += 1\n",
    "        while j < n2:\n",
    "            ans.append([nums2[j][0], nums2[j][1]])\n",
    "            j += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, A: List[List[int]], B: List[List[int]]) -> List[List[int]]:\n",
    "        return list(sorted((Counter(dict(A)) + Counter(dict(B))).items()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        d = collections.defaultdict(int)\n",
    "        for i, v in nums1:\n",
    "            d[i] += v\n",
    "        \n",
    "        for i, v in nums2:\n",
    "            d[i] += v\n",
    "        \n",
    "        ret = []\n",
    "        for i, v in d.items():\n",
    "            ret.append((i, v))\n",
    "\n",
    "        ret.sort()\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        a=[]\n",
    "        for i in nums1:\n",
    "            if i[0]  not in a:\n",
    "                a.append(i[0])\n",
    "        for i in nums2:\n",
    "            if i [0] not in a:\n",
    "                a.append(i[0])\n",
    "        maxnum=max(a)\n",
    "        nums=[]\n",
    "        dic={}\n",
    "        a.sort()\n",
    "        for i in range(0,len(a)):\n",
    "            dic[a[i]]=0\n",
    "        nums3=nums1+nums2\n",
    "        for i in range(0,len(nums3)):\n",
    "            dic[nums3[i][0]]+=nums3[i][1]\n",
    "        for i in dic.keys():\n",
    "            nums.append([i,dic[i]])\n",
    "        return nums\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        nums1.extend(nums2)\n",
    "        hash_d = {}\n",
    "        for i in nums1:\n",
    "            hash_d[i[0]] = hash_d.get(i[0],0) + i[1]\n",
    "        num = [[k,v] for k, v in hash_d.items()]\n",
    "        return sorted(num,key = lambda x:x[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        dictsum = {}\n",
    "        for x,y in nums1:\n",
    "            dictsum[x] = y\n",
    "        for x,y in nums2:\n",
    "            if x in dictsum:\n",
    "                dictsum[x] += y\n",
    "            else:\n",
    "                dictsum[x] = y\n",
    "        a = sorted(dictsum.items(), key=lambda item: item[0])\n",
    "        return [[x,y] for x,y in a]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        m = Counter() \n",
    "        for x,y in nums1:\n",
    "            m[x]+=y\n",
    "        for x,y in nums2:\n",
    "            m[x]+=y\n",
    "        return sorted(m.items())\n",
    "\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        ans=[]\n",
    "        m,n=len(nums1),len(nums2)\n",
    "        i,j=0,0\n",
    "        while i<m and j<n:\n",
    "            if nums1[i][0] == nums2[j][0]:\n",
    "                ans.append([nums1[i][0],nums1[i][1]+nums2[j][1]])\n",
    "                i+=1\n",
    "                j+=1\n",
    "            elif nums1[i][0] < nums2[j][0]:\n",
    "                ans.append(nums1[i])\n",
    "                i+=1\n",
    "            elif nums1[i][0] > nums2[j][0]:\n",
    "                ans.append(nums2[j])\n",
    "                j+=1\n",
    "        if i<m:\n",
    "            ans.extend(nums1[i:])\n",
    "        if j<n:\n",
    "            ans.extend(nums2[j:])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        cal = [0] * 1005\n",
    "        for l in nums1:\n",
    "            k = l[0]\n",
    "            v = l[1]\n",
    "            cal[k] += v\n",
    "        for l in nums2:\n",
    "            k = l[0]\n",
    "            v = l[1]\n",
    "            cal[k] += v\n",
    "        ans = []\n",
    "        for i, v in enumerate(cal):\n",
    "            if v != 0:\n",
    "                ans.append([i, v])\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        dict = {}\n",
    "        res = []\n",
    "        for id, val in nums1:\n",
    "            dict[id] = val\n",
    "        for id, val in nums2:\n",
    "            if id not in dict:\n",
    "                dict[id] = val\n",
    "            else:\n",
    "                dict[id] += val\n",
    "        for k, v in dict.items():\n",
    "            res.append([k, v])\n",
    "        return sorted(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        d = collections.Counter()\n",
    "        for x,y in nums1:d[x] += y\n",
    "        for x,y in nums2:d[x] += y\n",
    "        return list(sorted(d.items()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        m = len(nums1)\n",
    "        n = len(nums2)\n",
    "        i,j =0,0\n",
    "        ans = []\n",
    "        while i<m or j<n:\n",
    "            if i<m and j<n:\n",
    "                if nums1[i][0]==nums2[j][0]:\n",
    "                    ans.append([nums1[i][0],nums1[i][1]+nums2[j][1]])\n",
    "                    i+=1\n",
    "                    j+=1\n",
    "                elif nums1[i][0]<nums2[j][0]:\n",
    "                    ans.append(nums1[i][:])\n",
    "                    i+=1\n",
    "                else:\n",
    "                    ans.append(nums2[j][:])\n",
    "                    j+=1\n",
    "            else:\n",
    "                if i<m:\n",
    "                    ans.extend(nums1[i:])\n",
    "                    break\n",
    "                else:\n",
    "                    ans.extend(nums2[j:])\n",
    "                    break\n",
    "        \n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        d = collections.defaultdict(int)\n",
    "        for x, y in nums1:\n",
    "            d[x] += y\n",
    "        for x, y in nums2:\n",
    "            d[x] += y\n",
    "        # print(d)\n",
    "        res = list(d.items())\n",
    "        res.sort()\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        i = j = 0\n",
    "        m, n = len(nums1), len(nums2)\n",
    "        ans = []\n",
    "        while i < m or j < n:\n",
    "            if i == m:\n",
    "                ans += nums2[j:]\n",
    "                break\n",
    "            if j == n:\n",
    "                ans += nums1[i:]\n",
    "                break\n",
    "            if nums1[i][0] == nums2[j][0]:\n",
    "                ans.append([nums1[i][0], nums1[i][1] + nums2[j][1]])\n",
    "                i += 1\n",
    "                j += 1\n",
    "            elif nums1[i][0] > nums2[j][0]:\n",
    "                ans.append(nums2[j])\n",
    "                j += 1\n",
    "            elif nums1[i][0] < nums2[j][0]:\n",
    "                ans.append(nums1[i])\n",
    "                i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        HashMap = defaultdict()\n",
    "        for num in nums1:\n",
    "            if num[0] not in HashMap:\n",
    "                HashMap[num[0]] = num[1]\n",
    "            else:\n",
    "                HashMap[num[0]] += num[1]\n",
    "        for num in nums2:\n",
    "            if num[0] not in HashMap:\n",
    "                HashMap[num[0]] = num[1]\n",
    "            else:\n",
    "                HashMap[num[0]] += num[1]\n",
    "        ans = []\n",
    "        for key, value in HashMap.items():\n",
    "            ans.append([key, value])\n",
    "        ans.sort(key = lambda x: x[0])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        ans = []\n",
    "        i = j = 0\n",
    "        m, n = len(nums1), len(nums2)\n",
    "        while i < m and j < n:\n",
    "            while i < m and nums1[i][0] < nums2[j][0]:\n",
    "                ans.append(nums1[i])\n",
    "                i += 1\n",
    "            while i < m and j < n and nums2[j][0] < nums1[i][0]:\n",
    "                ans.append(nums2[j])\n",
    "                j += 1\n",
    "            if i < m and j < n and nums1[i][0] == nums2[j][0]:\n",
    "                ans.append([nums1[i][0], nums1[i][1] + nums2[j][1]])\n",
    "                i += 1\n",
    "                j += 1\n",
    "        return ans + nums1[i:] + nums2[j:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        i,j = 0,0\n",
    "        res = []\n",
    "        while i < len(nums1) and j < len(nums2):\n",
    "            if nums1[i][0] == nums2[j][0]:\n",
    "                res.append([nums1[i][0],nums1[i][1]+nums2[j][1]])\n",
    "                i += 1\n",
    "                j += 1\n",
    "            elif nums1[i][0] < nums2[j][0]:\n",
    "                res.append(nums1[i])\n",
    "                i += 1\n",
    "            else:\n",
    "                res.append(nums2[j])\n",
    "                j += 1\n",
    "        for k in range(i,len(nums1)):\n",
    "            res.append(nums1[k])\n",
    "        for k in range(j,len(nums2)):\n",
    "            res.append(nums2[k])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:\n",
    "        return list(sorted((Counter(dict(nums1)) + Counter(dict(nums2))).items()))"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
